﻿using Microsoft.Win32;
using System;
using System.IO;
using System.Windows.Forms;

namespace WorldRacing2.Configuration
{
    internal partial class WorldRacing2Folder : UserControl
    {
        #region Private Fields

        private WorldRacing2ConfigurationData configuration;
        private FolderBrowserDialog folderBrowser;

        #endregion Private Fields

        #region Public Constructors

        public WorldRacing2Folder(WorldRacing2ConfigurationData configuration)
        {
            InitializeComponent();
            this.configuration = configuration;
        }

        #endregion Public Constructors

        #region Internal Methods

        /// <summary>
        /// Finds World racing 2 installation folder by searcing in the registry.
        /// </summary>
        internal void FindWR2Folder()
        {
            if (!Directory.Exists(this.configuration.Wr2DirectoryFolder))
            {
                string subkey = @"SOFTWARE\Synetic\World Racing 2";

                if (!SubKeyExist(subkey))
                {
                    subkey = @"SOFTWARE\Wow6432Node\Synetic\World Racing 2";
                }

                if (SubKeyExist(subkey))
                {
                    RegistryKey myKey = Registry.LocalMachine.OpenSubKey(subkey);
                    string[] subkeys = myKey.GetSubKeyNames();
                    this.configuration.Wr2DirectoryFolder = (string)myKey.GetValue("InstallDir");
                }
            }
        }

        internal void ImportTexturesFromWR2(string sceneryEditorTextureFolder)
        {
            if (Directory.Exists(this.configuration.Wr2DirectoryFolder))
            {
                string[] wr2Sceneries = new string[] {
                                                   "Autobahn" , "AutobahnCT","CT_Autobahn","Landstr","NStadt","StadtCT2", // Cobra 11 Maps
                                                   "Egypten", "Hawaii", "HHeimRing", "Italien", "Miami", "Testcenter", // World Racing 2 Maps
                                                   "Alpen","Australien","Japan","Nevada","Stadt", // World Racing Maps
                                                   "DoesnotExist"
            };

                for (int i = 0; i < wr2Sceneries.Length; i++)
                {
                    ImportTexturesFromFolder(sceneryEditorTextureFolder,
                        this.configuration.Wr2DirectoryFolder + "\\Scenarios\\" + wr2Sceneries[i] + "\\V1\\Textures\\");
                }
            }
            else
            {
                MessageBox.Show("No World Racing 2 folder is set.");
            }
        }

        #endregion Internal Methods

        #region Private Methods

        private void button_ImportWr2Textures_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            button.Enabled = false;
            ImportTexturesFromWR2(Application.StartupPath + "\\Texture");

            button.Text = "Done";
            button.Enabled = true;
        }

        private void button_Search_Click(object sender, EventArgs e)
        {
            if (this.folderBrowser.ShowDialog() == DialogResult.OK)
            {
                this.configuration.Wr2DirectoryFolder = this.folderBrowser.SelectedPath;
                this.configuration.Save();
                this.textBox_Wr2Path.Text = this.configuration.Wr2DirectoryFolder;
            }
        }

        private void ImportTexturesFromFolder(string destination, string folder)
        {
            if (!Directory.Exists(folder))
                return;

            string distinationPath = destination + "\\";

            if (!Directory.Exists(distinationPath))
                Directory.CreateDirectory(distinationPath);

            string[] files = Directory.GetFiles(folder, "*.ptx");

            // Copy textures
            for (int i = 0; i < files.Length; i++)
            {
                string shortFileName = Path.GetFileName(files[i]);
                this.button_ImportWr2Textures.Text = shortFileName;
                Application.DoEvents();
                string fileToCopy = distinationPath + shortFileName;

                if (!File.Exists(fileToCopy))
                    File.Copy(files[i], fileToCopy, false);
            }
        }

        private bool SubKeyExist(string Subkey)
        {
            // Check if a Subkey exist
            RegistryKey myKey = Registry.LocalMachine.OpenSubKey(Subkey);
            if (myKey == null)
                return false;
            else
                return true;
        }

        private void WorldRacing2Folder_Load(object sender, EventArgs e)
        {
            // Init Folderbrowser
            this.folderBrowser = new FolderBrowserDialog();
            this.folderBrowser.SelectedPath = this.configuration.Wr2DirectoryFolder;

            // Set path to textbox.
            this.textBox_Wr2Path.Text = this.configuration.Wr2DirectoryFolder;

            // search wr2 folder if it isn´t set already.
            if (this.configuration.Wr2DirectoryFolder == string.Empty)
                button_Search_Click(null, null);
        }

        #endregion Private Methods
    }
}